﻿using System.Linq;
using FitNesseRunner;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FitNesseRunnerTests
{
	[TestClass]
	public class DecisionTableHandlerShould
	{
		[TestMethod]
		public void InstantiateClass()
		{
			var instance = new object();
			var reflectorProvider = new FakeReflectionProvider();
			reflectorProvider.SetConstructor("ClassName", args => instance);

			var table = new FakeTable(new[] {"ClassName"},
			                          new[] {"Dummy column header"});
			var handler = new DecisionTableHandler(null, reflectorProvider);
			handler.Handle(table);

			Assert.AreSame(instance, reflectorProvider.InstantiatedObject);
		}

		[TestMethod]
		public void PassArgumentsToConstructor()
		{
			var instance = new object();
			var reflectorProvider = new FakeReflectionProvider();
			
			var table = new FakeTable(new[] { "ClassName", "arg1", "arg2"},
									  new[] { "Dummy column header" });

			reflectorProvider.SetConstructor("ClassName", args =>
			                                              	{
			                                              		Assert.AreEqual("arg1", args.ElementAt(0));
			                                              		Assert.AreEqual("arg2", args.ElementAt(1));
			                                              		return instance;
			                                              	});
			
			var handler = new DecisionTableHandler(null, reflectorProvider);
			handler.Handle(table);

			Assert.AreSame(instance, reflectorProvider.InstantiatedObject);
		}

		[TestMethod]
		public void TrimsArgumentsToConstructor()
		{
			var instance = new object();
			var reflectorProvider = new FakeReflectionProvider();

			var table = new FakeTable(new[] { "ClassName", "  arg1   ", "   arg2   " },
									  new[] { "Dummy column header" });

			reflectorProvider.SetConstructor("ClassName", args =>
			{
				Assert.AreEqual("arg1", args.ElementAt(0));
				Assert.AreEqual("arg2", args.ElementAt(1));
				return instance;
			});

			var handler = new DecisionTableHandler(null, reflectorProvider);
			handler.Handle(table);

			Assert.AreSame(instance, reflectorProvider.InstantiatedObject);
		}
	}
}
